﻿#pragma once

#include "common.h"
#include "keyed.h"
#include "transform.h"
#include "target.h"
#include <vector>

namespace anip
{
	class GraphicsRenderContext
	{
		float _aspectRatio = 1;
		float _aspectScale = 1;
		RenderBuffer* _target = nullptr;

	public:
		Transform2D transform;

		float aspectRatio() const { return _aspectRatio; }
		float aspectScale() const { return _aspectScale; }

		GraphicsRenderContext() {}

		GraphicsRenderContext(invec2 viewport):
			_aspectScale(viewport.y), _aspectRatio(viewport.x / viewport.y)
		{ }

		void setViewport(invec2 v)
		{
			_aspectScale = v.y;
			_aspectRatio = v.x / v.y;
		}

		vec2 view2screen(invec2 p) const
		{
			vec2 pp = transform.transform(p);
			return vec2{ pp.x, _aspectScale - pp.y } * _aspectScale;
		}

		vec2 screen2view(invec2 p) const
		{
			return transform.undoTransform(vec2{ p.x, _aspectScale - p.y } / _aspectScale);
		}

		void beginRender(RenderBuffer& target);

		void endRender();
	};
}